package com.sxrong.carloan.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sxrong.carloan.R;
import com.sxrong.carloan.adapter.CarAdapter;
import com.sxrong.carloan.adapter.MainViewPagerAdapter;
import com.sxrong.carloan.dto.CarInfoBean;
import com.sxrong.carloan.dto.CarInfoModel;
import com.sxrong.carloan.fragment.CarFragment;
import com.sxrong.carloan.net.ApiService;
import com.sxrong.carloan.net.MyCallBack;
import com.sxrong.carloan.net.UserManage;
import com.sxrong.carloan.net.XUtils;
import com.sxrong.carloan.utils.AppCacheSharedPreferences;
import com.sxrong.carloan.utils.Config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends FragmentActivity implements CarFragment.ClickListener{
    private static final int LOGIN_REQUEST_CODE = 11;
    private static final int CARD_INFO_CODE = 12;

    private static final int EDIT_REQUEST_CODE = 101;

    private TextView title_tv;
    private FloatingActionButton mMenuImageView;
    private ImageView mAddCarBtn;
    private ImageView mLogoutBtn;
    private TabLayout mTabLayout;

    private TextView log_out_tv;
    private TextView add_car_tv;

    ViewPager viewPager;
    private CarFragment carFragment1 , carFragment2 ,carFragment3;

    private String[] mtabLists = {"审核成功" , "审核中","审核失败"};
    private List<Fragment> mFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        title_tv = findViewById(R.id.title_tv);
        mMenuImageView = findViewById(R.id.menu_iv);
        mAddCarBtn = findViewById(R.id.add_car_btn);
        mLogoutBtn = findViewById(R.id.log_out_iv);
        mTabLayout = findViewById(R.id.tab_layout);
        log_out_tv = findViewById(R.id.log_out_tv);
        add_car_tv = findViewById(R.id.add_car_tv);
        viewPager = findViewById(R.id.view_pager);
        title_tv.setText("车辆信息");

        initAction();
        if (AppCacheSharedPreferences.getCacheBoolean(Config.IS_LOGIN, false)) {
            initTabs();
        } else {
            startActivityForResult(new Intent(this, LoginActivity.class), LOGIN_REQUEST_CODE);
        }
    }

    private void initTabs(){
        mFragments = new ArrayList<>();
        carFragment1 = CarFragment.newInstance(1);
        //carFragment2 = CarFragment.newInstance(0);
        //carFragment3 = CarFragment.newInstance(2);

        mFragments.add(carFragment1);
       // mFragments.add(carFragment2);
       // mFragments.add(carFragment3);
        MainViewPagerAdapter adapter = new MainViewPagerAdapter(getSupportFragmentManager() , mtabLists ,mFragments);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(adapter.getCount());
        mTabLayout.setupWithViewPager(viewPager);
        mTabLayout.setVisibility(View.GONE);
        viewPager.setCurrentItem(0, false);
    }

    private void initAction() {
       /* mMenuImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAddCarBtn.getVisibility() == View.GONE) {
                    startAnimatior1();
                } else {
                    startAnimatior2();
                }
            }
        });*/

        mMenuImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(MainActivity.this , CarInfoActivity.class) ,EDIT_REQUEST_CODE );
            }
        });

        log_out_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this).setTitle("温馨提示").setMessage("您确定要退出登录吗?")
                        .setPositiveButton("我意已决", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                UserManage.clearUserInfo();
                                Toast.makeText(MainActivity.this, "您已成功注销", Toast.LENGTH_SHORT).show();
                                startActivityForResult(new Intent(MainActivity.this, LoginActivity.class), LOGIN_REQUEST_CODE);
                            }
                        }).setNegativeButton("手贱点错了", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                builder.create().show();
            }
        });

        add_car_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this , SearchActivity.class));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }




    /*@SuppressLint("ObjectAnimatorBinding")
    public void startAnimatior1() {
        ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(mMenuImageView, "rotation", 0f, 135f);
        rotateAnimator.setDuration(500);
        rotateAnimator.start();
        rotateAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mAddCarBtn.setVisibility(View.VISIBLE);
                mLogoutBtn.setVisibility(View.VISIBLE);
                ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(mAddCarBtn, "alpha", 0f, 1f);
                alphaAnimation.setDuration(500);
                ObjectAnimator translation = ObjectAnimator.ofFloat(mAddCarBtn, "translationX", 0f, -500f);
                translation.setDuration(500);


                ObjectAnimator alphaAnimation1 = ObjectAnimator.ofFloat(mLogoutBtn, "alpha", 0f, 1f);
                alphaAnimation1.setDuration(500);
                ObjectAnimator translationY = ObjectAnimator.ofFloat(mLogoutBtn, "translationY", 0f, -500f);
                translationY.setDuration(500);


                AnimatorSet animationSet1 = new AnimatorSet();
                animationSet1.playTogether(alphaAnimation, alphaAnimation1, translation, translationY);
                animationSet1.setDuration(500);
                animationSet1.start();

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        rotateAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Log.d("时间", (float) valueAnimator.getAnimatedValue() + "");
            }
        });
    }

    @SuppressLint("ObjectAnimatorBinding")
    public void startAnimatior2() {
        ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(mMenuImageView, "rotation", 135f, 270f);
        rotateAnimator.setDuration(500);
        rotateAnimator.start();
        rotateAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(mAddCarBtn, "alpha", 1f, 0f);
                alphaAnimation.setDuration(500);
                ObjectAnimator translation = ObjectAnimator.ofFloat(mAddCarBtn, "translationX", -500f, 0f);
                translation.setDuration(500);


                ObjectAnimator alphaAnimation1 = ObjectAnimator.ofFloat(mLogoutBtn, "alpha", 1f, 0f);
                alphaAnimation1.setDuration(500);
                ObjectAnimator translationY = ObjectAnimator.ofFloat(mLogoutBtn, "translationY", -500f, 0f);
                translationY.setDuration(500);


                AnimatorSet animationSet1 = new AnimatorSet();
                animationSet1.playTogether(alphaAnimation, alphaAnimation1, translation, translationY);
                animationSet1.setDuration(500);
                animationSet1.start();
                animationSet1.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        mAddCarBtn.setVisibility(View.GONE);
                        mLogoutBtn.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {
                        mAddCarBtn.setVisibility(View.GONE);
                        mLogoutBtn.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {

                    }
                });

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        rotateAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Log.d("时间", (float) valueAnimator.getAnimatedValue() + "");
            }
        });
    }*/

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LOGIN_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                initTabs();
            } else if (resultCode == RESULT_CANCELED) {
                finish();
            }
        }
        if(requestCode == EDIT_REQUEST_CODE){
            carFragment1.getCardInfo();
            //carFragment2.getCardInfo();
           // carFragment3.getCardInfo();
        }
    }

    @Override
    public void clickItem(CarInfoBean bean) {
        Intent intent = new Intent(this, CarInfoActivity.class);
        intent.putExtra("data" , bean);
        startActivityForResult(intent , EDIT_REQUEST_CODE);
    }

    @Override
    public void addItem() {

    }
}
